<?php
// 这是系统自动生成的公共文件

if(!function_exists('random_string')) {
    function random_string($length = 16) {
        $str = bin2hex(random_bytes($length << 3)) . hrtime(true) . uniqid(time()). random_int(1000, 9999);
        $str = str_shuffle($str);

        $len = strlen($str);

        $strArr = [];
        for($i = 0; $i < $length; $i++) {
            $strArr[] = $str[mt_rand(0, $len - 1)];
        }

        return implode($strArr);
    }
}

if(!function_exists('nullEmpty')) {
    function nullEmpty($value) {
        if(!is_array($value)) {
            $value = trim($value);
            return $value === null || $value === '' || $value === false;
        }

        return count($value) === 0;
    }
}

if(!function_exists('buildFullUrl')) {
    function buildFullUrl($baseUrl, $resourcePath) {
        $baseUrl = strtr($baseUrl, ['\\' => '/']);
        $resourcePath = strtr($resourcePath, ['\\' => '/']);

        // 如果资源路径是完整URL则直接返回
        if (parse_url($resourcePath, PHP_URL_SCHEME)) {
            return $resourcePath;
        }

        // 标准化基础URL
        $parsedBase = parse_url(rtrim($baseUrl, '/'));
        $scheme = $parsedBase['scheme'] ?? 'http';
        $host = $parsedBase['host'] ?? '';
        $basePath = $parsedBase['path'] ?? '';

        // 标准化资源路径
        $resourcePath = ltrim($resourcePath, '/');

        // 拼接完整URL
        return sprintf('%s://%s%s/%s',
            $scheme,
            $host,
            $basePath ? '/' . trim($basePath, '/') : '',
            $resourcePath
        );
    }
}